<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>quickSort</title>
  </head>
  <body>
    <script>
      function quickSort(array) {
        let length = array.length;
        if (length <= 1) return array;
        let pivotIndex = Math.floor(length / 2),
          pivot = array.splice(pivotIndex, 1)[0],
          left = [],
          right = [];
        for (let i = 0; i < length; i++) {
          if (array[i] < pivot) {
            left.push(array[i]);
          } else if (array[i] > pivot) {
            right.push(array[i]);
          }
        }
        return quickSort(left).concat(pivot, quickSort(right));
      }

      let arr = [123, 14, 521, 8, 241, 943, 19, 56, 31];
      console.log(quickSort(arr));
    </script>
  </body>
</html>
